import CombatBase from "@/Battle/CombatBase.js";
import CombatEventBus from "@/Battle/CombatEventBus.js";
import { BuffEventType } from "@/Battle/Event/BuffEvent.js";
import { CombatEventPool } from "@/Battle/Event/CombatEventPool.js";
/**
 * 1 负面的
 * 2 回合限制的
 * 4 次数限制
 * 8 不可移除
 * 
 * 0x00 00 00 00
 */
export const BuffTrait={
    negative:0x00000001,
    turnlimit:0x00000002,
    timeslimit:0x00000004,
    unremove:0x00000008
}

export const BuffReason={
    Remove:{
        removed:1,
        turnlimit:2,
        timeslimit:3,
        damage:4
    }
}


export class Buff extends CombatBase{
    constructor(id,owner,source){
        super();
        this.id=id;

        this.source=source;
        this.owner=owner;
        
        this.trait=0;
    }
    
    onAdded(){}
    onRemoved(){}


    request(type,launcher,reason,onaccept,onreject){
        CombatEventBus.dispatch(
            CombatEventPool.newCombatEvent(type,launcher,{
                source:this.source,
                target:this.owner
            },reason),
            onaccept,
            onreject
        );
    }

    requestAdd(launcher,reason,onaccept,onreject){
        this.request(BuffEventType.Add.Request,launcher,reason,onaccept,onreject);
    }
    completeAdd(launcher,reason,onaccept,onreject){
        this.request(BuffEventType.Add.Complete,launcher,reason,onaccept,onreject);
    }
    requestRemove(launcher,reason,onaccept,onreject){
        this.request(BuffEventType.Remove.Request,launcher,reason,onaccept,onreject);
    }
    completeRemove(launcher,reason,onaccept,onreject){
        this.request(BuffEventType.Remove.Complete,launcher,reason,onaccept,onreject);
    }

    setTrait(...args){this.trait=args.reduce((r,trait)=>r|trait,0);}
    addTrait(trait){this.trait|=trait;}
    removeTrait(trait){this.trait&=(~trait);}
    isNegative(){return this.trait&BuffTrait.negative;}
    isTurnLimit(){return this.trait&BuffTrait.turnlimit;}
    isTimesLimit(){return this.trait&BuffTrait.timeslimit;}
    isUnremovable(){return this.trait&BuffTrait.unremove}
}